package com.example.smartcitydemo.ui.job.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.HomeBannerBean
import com.example.smartcitydemo.bean.JobListBean
import com.example.smartcitydemo.databinding.FragmentJobHomeBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ext.onActionSearch
import com.example.smartcitydemo.ext.onLoad
import com.example.smartcitydemo.ui.job.JobListAdapter
import com.example.smartcitydemo.ui.job.JobSearchActivity
import com.example.smartcitydemo.viewModel.home.HomeViewModel
import com.example.smartcitydemo.viewModel.job.WorkViewModel

import com.google.android.material.chip.Chip
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.job.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/11/18 9:34
 * @Email: 1919796283@qq.com
 */
class JobHomeFragment : BaseFragment<FragmentJobHomeBinding>(R.layout.fragment_job_home) {
    private val homeViewModel: HomeViewModel by viewModels()
    private val workViewModel: WorkViewModel by viewModels()
    private val jobListAdapter: JobListAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        JobListAdapter().apply {
            init()
            setDiffCallback(JobListBean.jobCallBack)
            setOnItemClickListener { adapter, view, pos ->
                val item = getItem(pos)
                val bundle = Bundle().apply {
                    putInt("id", item.id)
                    putInt("cpId", item.companyId)
                }
                this@JobHomeFragment.findNavController()
                    .navigate(R.id.action_navigation_work_home_to_navigation_job_deliver, bundle)
            }
        }
    }

    override fun initData() {
        homeViewModel.getHomeBanner()
        workViewModel.getJobType()
        workViewModel.getJobList(0)
    }

    override fun initView(bindView: FragmentJobHomeBinding, savedInstanceState: Bundle?) {
        bindView.apply {
            typeBarWork.typeBarText.text = "职位列表"
            typeBarWorkList.typeBarText.text = "职位信息"
            searchViewWork.onActionSearch { string ->

                val intent = Intent(
                    this@JobHomeFragment.requireContext(),
                    JobSearchActivity::class.java
                ).apply {
                    putExtra("search", string)
                }
                this@JobHomeFragment.requireActivity().startActivity(intent)
            }
            workChipGroup.setOnCheckedChangeListener { group, checkedId ->
                if (checkedId != -1) {
                    val chip = group.findViewById(checkedId) as Chip
                    val jobType = chip.tag as String
                    val jobTypeInt = jobType?.toInt() ?: 0
                    //不是换一换
                    if (jobTypeInt != -1) {
                        workViewModel.getJobList(jobTypeInt)
                    }
                }
            }
            chipRefresh.setOnClickListener {
                //更换首页显示的职业类型
                workViewModel.getJobType()
            }
            workRecycle.apply {
                adapter = jobListAdapter
            }
        }
    }

    override fun addObservers(bindView: FragmentJobHomeBinding) {
        homeViewModel.homeBannerBean.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success -> {
                    Log.d(TAG, "addObservers: $it")
                    bindView.workBanner.setAdapter(object :
                        BannerImageAdapter<HomeBannerBean>(it.data!!.toMutableList()) {
                        override fun onBindView(
                            holder: BannerImageHolder,
                            data: HomeBannerBean,
                            position: Int,
                            size: Int,
                        ) {
                            holder.imageView.onLoad(data.advImg)
                        }

                    })
                }
            }
        }
        workViewModel.jobTypeLiveData.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success -> {
                    if (it is Resource.Success) {
                        it.data!!.also { list ->
                            //出了shufflt
                            val randomList = list.shuffled()
                            bindView.workChipGroup.run {
                                if (childCount > 2)
                                    removeViews(2, 5)
                            }
                            for (i in (0..4)) {
                                randomList[i].also {
                                    val newChip = Chip(bindView.workChipGroup.context).apply {
                                        text = it.professionName
                                        tag = "${it.id}"
                                    }
                                    bindView.workChipGroup.addView(newChip, i + 2)
                                }
                            }
                        }
                    }
                }
            }
        }
        workViewModel.jobListData.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success ->
                    jobListAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Failure ->
                    jobListAdapter.apply {
                        setDiffNewData(null)
                        setEmptyView(R.layout.error_layout)
                    }
                is Resource.Loading ->
                    jobListAdapter.setEmptyView(R.layout.loading_layout)
            }
        }
    }
}